const app = getApp()
import {
  HomeIndex
} from 'index-modal.js';
import {
  Config
} from '../../../utils/config.js';
var Function = require("../../../utils/function.js");
var index = new HomeIndex(); //实例化 首页 对象
Page({
  data: {
    conlist:[],
    strl:'',
    type:2,
    price:0,
    type_ed:1,
    arr:['门店抵用券','新人优惠券','个人档案红包','邀请有礼']
  },

  onLoad: function (options) {
    if (!wx.getStorageSync('members')) {
      wx.showModal({
        title: '提示',
        content: '你还未授权是否前往授权',
        success(res) {
          if (res.confirm) {
            wx.navigateTo({
              url: '/pages/auth/auth',
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
    }else{
      let type = options.type || 2;
      let price = options.price || 0;
      this.setData({ type, price})
      this._coupon()
    }
  },

  onShow:function(options){
    if (options&&options.scene) {
      Object.assign(this.options, getScene(options.scene)) // 获取二维码参数，绑定在当前this.options对象上
    }
    if(this.options){
        this.temporary(this.options)
    }
    //var obj = wx.getLaunchOptionsSync()
    this.setData([])
    this._coupon()
  },

  /**
   * 获取小程序二维码参数
   * @param {String} scene 需要转换的参数字符串
   */
  getScene: function(scene = "") {
    if (scene == "") return {}
    let res = {}
    let params = decodeURIComponent(scene).split("&")
    params.forEach(item => {
      let pram = item.split("=")
      res[pram[0]] = pram[1]
    })
    return res
  },

  _coupon(){
    let that =this;
    let id = wx.getStorageSync('members').id;
    let data ={
      id,
      type: that.data.type_ed
    }
    index.coupon(data,(res)=>{
      if(res.code == 200){
       that.setData({
         conlist:res.data
       })
       wx.stopPullDownRefresh()
      }
    })
  },
  keyvalue(e){
    this.setData({
     strl:e.detail.value
    })
  },
  lgerve(){
    let token = wx.getStorageSync('members').token || '';
    if (token != '') {
      this.data.strl != "" ? this.setcoupon() : wx.showToast({
        title: '劵码不能为空',
        duration: 1000,
        icon: 'none'
      });
    } 
  },
  setcoupon(){
    let that =this;
    let data ={
      uid:wx.getStorageSync('members').id,
      code:that.data.strl
    };
    index.exchange(data,(res)=>{
      if(res.code == 200){
        wx.showToast({
          title: res.msg,
          duration: 1000,
          icon: 'none'
        });
        that._coupon()
      }else{
        wx.showToast({
          title: res.msg,
          duration: 1000,
          icon: 'none'
        });
      }
    })
  },
  linkeTo(e){
    let str = JSON.stringify(this.data.conlist[parseInt(e.currentTarget.dataset.index)]);
    wx.navigateTo({
      url: '../coupon2/coupon2?lenove=' +str
    })
  },
  mommeng(e){
    let index = parseInt(e.currentTarget.dataset.indexd);
    var that =this;
    if(this.data.type == 2){
      wx.switchTab({
        url: '/pages/shouye/index/index'
      })
    }else{
      if (this.data.price < this.data.conlist[index].man){
        wx.showToast({
          title: '不符合满减要求',
          duration:1000,
          icon:'none'
        })
      }else{
        wx.showToast({
          title: '使用成功',
          duration:1000
        })
        setTimeout(()=>{
          wx.setStorageSync('orderid', that.data.conlist[index].id)
          wx.navigateBack()
        },1300)
       
      }
    }
  },

  //扫码领优惠劵
  temporary(e){
    //console.log(e)
    if (Object.keys(e).length != 0) {
      wx.showModal({
        title: '领取',
        content: '有可领取的优惠券，请确认是否领取！',
        success(res) {
          if (res.confirm) {
            wx.request({
              url: Config.restUrl+'goods/getTemporary',
              method: 'POST',
              data: {
                uid:wx.getStorageSync('members').id,
                method: e.method,
                verify: e.verify,
                coupon_id: e.coupon_id,
                mold: e.mold,
              },
              header: {
                'content-type': 'application/json' // 默认
              },
              success (res) {
                if (res.data.code == 200) {
                  wx.showLoading({
                    title: '领取成功！',
                  })
                  setTimeout(function () {
                    wx.hideLoading()
                  }, 1000)
                  wx.startPullDownRefresh()//下拉刷新数据
                }
              }
            })
          } else if (res.cancel) {
            console.log('用户点击取消')
          }
        },
      })
    }
  },

  //下拉刷新
  onPullDownRefresh: function() {
    this.updateBlogs()
  },
  updateBlogs: function () {
    this._coupon()
  }
})
